package cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroupuser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroupuser.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.researchgroupuser.ResearchgroupUserDO;
import cn.iocoder.yudao.module.famous.doctor.convert.researchgroupuser.ResearchgroupUserConvert;
import cn.iocoder.yudao.module.famous.doctor.service.researchgroupuser.ResearchgroupUserService;

@Tag(name = "管理后台 - 课题组-用户关联")
@RestController
@RequestMapping("/famous.doctor/researchgroup-user")
@Validated
public class ResearchgroupUserController {

    @Resource
    private ResearchgroupUserService researchgroupUserService;

    @PostMapping("/create")
    @Operation(summary = "创建课题组-用户关联")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:create')")
    public CommonResult<Long> createResearchgroupUser(@Valid @RequestBody ResearchgroupUserCreateReqVO createReqVO) {
        return success(researchgroupUserService.createResearchgroupUser(createReqVO));
    }
    @PostMapping("/createList")
    @Operation(summary = "批量创建课题组-用户关联")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:createList')")
    public CommonResult<Long> createResearchgroupUserList(@Valid @RequestBody List<ResearchgroupUserCreateReqVO> createReqVO) {
        return success(researchgroupUserService.createResearchgroupUserList(createReqVO));
    }
    @PutMapping("/update")
    @Operation(summary = "更新课题组-用户关联")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:update')")
    public CommonResult<Boolean> updateResearchgroupUser(@Valid @RequestBody ResearchgroupUserUpdateReqVO updateReqVO) {
        researchgroupUserService.updateResearchgroupUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课题组-用户关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:delete')")
    public CommonResult<Boolean> deleteResearchgroupUser(@RequestParam("id") Long id) {
        researchgroupUserService.deleteResearchgroupUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课题组-用户关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:query')")
    public CommonResult<ResearchgroupUserRespVO> getResearchgroupUser(@RequestParam("id") Long id) {
        ResearchgroupUserDO researchgroupUser = researchgroupUserService.getResearchgroupUser(id);
        return success(ResearchgroupUserConvert.INSTANCE.convert(researchgroupUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课题组-用户关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:query')")
    public CommonResult<List<ResearchgroupUserRespVO>> getResearchgroupUserList(@RequestParam("ids") Collection<Long> ids) {
        List<ResearchgroupUserDO> list = researchgroupUserService.getResearchgroupUserList(ids);
        return success(ResearchgroupUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课题组-用户关联分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:query')")
    public CommonResult<PageResult<ResearchgroupUserRespVO>> getResearchgroupUserPage(@Valid ResearchgroupUserPageReqVO pageVO) {
        PageResult<ResearchgroupUserDO> pageResult = researchgroupUserService.getResearchgroupUserPage(pageVO);
        return success(ResearchgroupUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课题组-用户关联 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:researchgroup-user:export')")
    @OperateLog(type = EXPORT)
    public void exportResearchgroupUserExcel(@Valid ResearchgroupUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ResearchgroupUserDO> list = researchgroupUserService.getResearchgroupUserList(exportReqVO);
        // 导出 Excel
        List<ResearchgroupUserExcelVO> datas = ResearchgroupUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课题组-用户关联.xls", "数据", ResearchgroupUserExcelVO.class, datas);
    }

}
